package com.ksy.ksypda;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.util.Log;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ksy.ksypda.libzxing.zxing.activity.CaptureActivity;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class MainActivity extends AppCompatActivity {

    @BindView(R.id.tv_zhang_tag_code)
    TextView tvZhangTagCode;
    @BindView(R.id.et_zhang_tag_code)
    EditText etZhangTagCode;
    @BindView(R.id.rl_zhang_detailed_content)
    RelativeLayout rlZhangDetailedContent;
    @BindView(R.id.tv_bao_tag_code)
    TextView tvBaoTagCode;
    @BindView(R.id.et_bao_tag_code)
    EditText etBaoTagCode;
    @BindView(R.id.rl_bao_detailed_content)
    RelativeLayout rlBaoDetailedContent;
    @BindView(R.id.tv_guanlian)
    TextView tvGuanlian;
    @BindView(R.id.tv_saomiao)
    TextView tvSaomiao;
    @BindView(R.id.ly_anniu)
    LinearLayout lyAnniu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showCamera();
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        etZhangTagCode.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        etBaoTagCode.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
    }

    //点击扫描二维码时，6.0以上系统需要动态申请权限
    @OnClick(R.id.tv_saomiao)
    public void onClick() {
        MainActivityPermissionsDispatcher.showCameraWithCheck(MainActivity.this);
        startActivity(new Intent(MainActivity.this, CaptureActivity.class));
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @NeedsPermission(Manifest.permission.CAMERA)
    void showCamera() {

    }

    @Subscriber(tag = "code", mode = ThreadMode.MAIN)
    private void receiveFromCap(CodeEvent info) {
        Log.d("输入", info.getCode());
        etZhangTagCode.setText(info.getCode());
    }

    //二维码图片
    @Subscriber(tag = "picture", mode = ThreadMode.MAIN)
    private void receiveFromDecode(Bitmap bitmap) {
        //mImage.setImageBitmap(bitmap);
    }


}
